package cn.qihua.dao.impl;

import java.util.List;

import org.hibernate.LockOptions;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.transaction.annotation.Transactional;

import cn.qihua.dao.ISysUserDAO;
import cn.qihua.model.SysUser;

/**
 * A data access object (DAO) providing persistence and search support for
 * SysUser entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see cn.qihua.model.SysUser
 * @author MyEclipse Persistence Tools
 */
@Transactional
public class SysUserDAO extends BaseHibernateDAO implements ISysUserDAO {
	private static final Logger log = LoggerFactory.getLogger(SysUserDAO.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.qihua.dao.impl.ISysUserDAO#setSessionFactory(org.hibernate.SessionFactory
	 * )
	 */
	protected void initDao() {
		// do nothing
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.qihua.dao.impl.ISysUserDAO#save(cn.qihua.model.SysUser)
	 */
	@Override
	public Boolean save(SysUser transientInstance) {
		log.debug("saving SysUser instance");
		try {
			getCurrentSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
		return true;
	}

	@Override
	public Boolean update(SysUser transientInstance) {
		log.debug("updataing SysUser instance");
		try {
			getCurrentSession().update(transientInstance);
			log.debug("updata successful");
		} catch (RuntimeException re) {
			log.error("updata failed", re);
			throw re;
		}
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.qihua.dao.impl.ISysUserDAO#delete(cn.qihua.model.SysUser)
	 */
	@Override
	public void delete(SysUser persistentInstance) {
		log.debug("deleting SysUser instance");
		try {
			getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.qihua.dao.impl.ISysUserDAO#findById(java.lang.Integer)
	 */
	@Override
	public SysUser findById(java.lang.Integer id) {
		log.debug("getting SysUser instance with id: " + id);
		try {
			SysUser instance = (SysUser) getCurrentSession().get(
					"cn.qihua.model.SysUser", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.qihua.dao.impl.ISysUserDAO#findByExample(cn.qihua.model.SysUser)
	 */
	@Override
	public List findByExample(SysUser instance) {
		log.debug("finding SysUser instance by example");
		try {
			List results = getCurrentSession()
					.createCriteria("cn.qihua.model.SysUser")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.qihua.dao.impl.ISysUserDAO#findByProperty(java.lang.String,
	 * java.lang.Object)
	 */
	@Override
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding SysUser instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from SysUser as model where model."
					+ propertyName + "= ?";
			Query queryObject = getCurrentSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.qihua.dao.impl.ISysUserDAO#findByOrganizationId(java.lang.Object)
	 */
	@Override
	public List findByOrganizationId(Object organizationId) {
		return findByProperty(ORGANIZATION_ID, organizationId);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.qihua.dao.impl.ISysUserDAO#findByRoleIds(java.lang.Object)
	 */
	@Override
	public List findByRoleIds(Object roleIds) {
		return findByProperty(ROLE_IDS, roleIds);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.qihua.dao.impl.ISysUserDAO#findByName(java.lang.Object)
	 */
	@Override
	public List findByName(Object name) {
		return findByProperty(NAME, name);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.qihua.dao.impl.ISysUserDAO#findByUsername(java.lang.Object)
	 */
	@Override
	public SysUser findByUsername(Object username) {
		List<SysUser> list = findByProperty(USERNAME, username);
		if (list.size() > 0) {
			SysUser u = list.get(0);
			return u;
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.qihua.dao.impl.ISysUserDAO#findByPassword(java.lang.Object)
	 */
	@Override
	public List findByPassword(Object password) {
		return findByProperty(PASSWORD, password);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.qihua.dao.impl.ISysUserDAO#findBySalt(java.lang.Object)
	 */
	@Override
	public List findBySalt(Object salt) {
		return findByProperty(SALT, salt);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.qihua.dao.impl.ISysUserDAO#findByPhone(java.lang.Object)
	 */
	@Override
	public List findByPhone(Object phone) {
		return findByProperty(PHONE, phone);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.qihua.dao.impl.ISysUserDAO#findByQq(java.lang.Object)
	 */
	@Override
	public List findByQq(Object qq) {
		return findByProperty(QQ, qq);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.qihua.dao.impl.ISysUserDAO#findByHi(java.lang.Object)
	 */
	@Override
	public List findByHi(Object hi) {
		return findByProperty(HI, hi);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.qihua.dao.impl.ISysUserDAO#findByEmail(java.lang.Object)
	 */
	@Override
	public List findByEmail(Object email) {
		return findByProperty(EMAIL, email);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.qihua.dao.impl.ISysUserDAO#findByLocked(java.lang.Object)
	 */
	@Override
	public List findByLocked(Object locked) {
		return findByProperty(LOCKED, locked);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.qihua.dao.impl.ISysUserDAO#findAll()
	 */
	@Override
	public List<SysUser> findAll() {
		log.debug("finding all SysUser instances");
		try {
			String queryString = "from SysUser";
			Query queryObject = getCurrentSession().createQuery(queryString);

			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.qihua.dao.impl.ISysUserDAO#merge(cn.qihua.model.SysUser)
	 */
	@Override
	public SysUser merge(SysUser detachedInstance) {
		log.debug("merging SysUser instance");
		try {
			SysUser result = (SysUser) getCurrentSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.qihua.dao.impl.ISysUserDAO#attachDirty(cn.qihua.model.SysUser)
	 */
	@Override
	public void attachDirty(SysUser instance) {
		log.debug("attaching dirty SysUser instance");
		try {
			getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.qihua.dao.impl.ISysUserDAO#attachClean(cn.qihua.model.SysUser)
	 */
	@Override
	public void attachClean(SysUser instance) {
		log.debug("attaching clean SysUser instance");
		try {
			getCurrentSession().buildLockRequest(LockOptions.NONE).lock(
					instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.qihua.dao.impl.ISysUserDAO#changeStatus(int, java.lang.Boolean)
	 */
	@Override
	public boolean changeLocked(int id, Boolean locked) {
		try {
			String queryString = "UPDATE SysUser SET locked=? WHERE id=?)";
			Query queryObject = getCurrentSession().createQuery(queryString);
			queryObject.setBoolean(0, locked);
			queryObject.setInteger(1, id);
			if (queryObject.executeUpdate() > 0) {
				return true;
			}
		} catch (RuntimeException re) {
			log.error("UPDATE SysUser failed", re);
			throw re;
		}
		return false;
	}

	public static ISysUserDAO getFromApplicationContext(ApplicationContext ctx) {
		return (ISysUserDAO) ctx.getBean("SysUserDAO");
	}
}